package com.ffdms.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.ffdms.db.DbHelper;
import com.ffdms.vo.Staff;

public abstract class AbstractClientService implements IClientService {
	private final String TAG = "AbstractClientService";
	private DbHelper helper = null;
	private SQLiteDatabase db = null;
	private Staff currentStaff;

	public AbstractClientService(Context context, Staff staff) {
		this.currentStaff = staff;
		helper = new DbHelper(context);
	}

	public SQLiteDatabase getDb() {
		db = helper.getWritableDatabase();
		return db;
	}

	public boolean insert(String tablename, ContentValues values) {
		db = helper.getWritableDatabase();
		long rowId = db.insert(tablename, null, values);
		if (rowId == -1) {
			db.close();
			Log.e(TAG, String.format("insert into {0} fail", tablename));
			return false;
		} else {
			db.close();
			return true;
		}
	}

	public boolean update(String tablename, ContentValues values,
			String whereClause, String[] whereArgs) {
		db = helper.getWritableDatabase();
		long rowId = db.update(tablename, values, whereClause, whereArgs);
		if (rowId == -1) {
			db.close();
			Log.e(TAG, String.format("insert into {0} fail", tablename));
			return false;
		} else {
			db.close();
			return true;
		}
	}

	public boolean delete(String tablename, String whereClause,
			String[] whereArgs) {
		db = helper.getWritableDatabase();
		long rowId = db.delete(tablename, whereClause, whereArgs);
		if (rowId == -1) {
			db.close();
			Log.e(TAG, String.format("insert into {0} fail", tablename));
			return false;
		} else {
			db.close();
			return true;
		}
	}

	/**
	 * after call this method, you must call close() to close the db connection
	 * */
	public Cursor getAll(String table, String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy) {
		db = helper.getWritableDatabase();
		Cursor cursor = db.query(table, columns, null, null, null, null, null);
		if (cursor.getCount() >= 1) {
			return cursor;
		} else {
			return cursor;
		}
	}

	public int getMaxId(String table){
		db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery(String.format("SELECT MAX(ID) FROM {0}",table), null);
		cursor.moveToFirst();
		int maxId = cursor.getInt(0);
		cursor.close();
		return maxId + 1;
	}
	/**
	 * must set current staff first this method will return the url with default
	 * parameters from some staff
	 * */
	@Override
	public List<BasicNameValuePair> GetServiceHttpPostBasicParameters() {
		if (this.currentStaff == null) {
			return null;
		} else {
			List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
			parameters.add(new BasicNameValuePair("StaffId", String
					.valueOf(this.currentStaff.getiD())));
			return parameters;
		}
	}

	/**
	 * must set current staff first this method will return the url with default
	 * parameters from some staff
	 * */
	@Override
	public String GetServiceHttpGetUrl() {
		if (this.currentStaff == null) {
			return null;
		} else {
			String urlWithParameters = String.format(this.URL + "?StaffId={0}",
					String.valueOf(this.currentStaff.getiD()));
			return urlWithParameters;
		}
	}

}
